package com.reiuy.service.impl;


import com.reiuy.dao.ClassDao;
import com.reiuy.dao.CourseDao;
import com.reiuy.entity.Class;
import com.reiuy.service.ClassService;
import com.reiuy.vo.ClassInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ClassServiceImpl implements ClassService {


    @Resource
    private ClassDao classDao;
    @Resource
    private CourseDao courseDao;

    @Override
    public ClassInfo showClass(Class c) {
        Class c_Temp = classDao.selectClassById(c);
        ClassInfo classInfo = new ClassInfo();
        String[] course1 = (c_Temp.getClasscourse()).split(",");
        System.out.println(Arrays.toString(course1));
        System.out.println(Integer.valueOf(course1[0]));
        System.out.println(course1[1]);
        System.out.println(course1.length);
        List<String> course2 = new ArrayList<>();
        for(int i = 0; i<course1.length;i++) {
            course2.add(courseDao.selectCourseById(Integer.valueOf(course1[i])));
        }
        System.out.println(course2);
        classInfo.setClassid(c_Temp.getClassid());
        classInfo.setClassname(c_Temp.getClassname());
        classInfo.setClassinfo(c_Temp.getClassinfo());
        classInfo.setClasscourses(course2);
        return classInfo;
    }

    @Override
    public List<Map<String, String>> showAllClass() {
        List<Map<String,String>> classes_t =  classDao.selectAllClasses();
        List<Map<String,String>> classes = new ArrayList<>();
        for(Map<String,String> cls :classes_t){
            String[] couresenum = (cls.get("classcourse")).split(",");
            List<String> coursenames = new ArrayList<>();
            int i = 0;
            for(i = 0; i<couresenum.length;i++) {
                coursenames.add(courseDao.selectCourseById(Integer.valueOf(couresenum[i])));
            }
            cls.put("coursenames",String.join(",",coursenames));
            System.out.println(cls);
            classes.add(cls);
        }
        System.out.println(classes);
        return classes;
    }
}
